const fs = require('fs')

const path = require('path')

function preDeep(dir, cb) {
    console.log(dir)
    fs.stat(dir, function (err, statObj) {
        if (statObj.isFile()) {
            console.log('object=>', dir, cb)
            fs.unlink(dir, cb)
        } else {
            fs.readdir(dir, function (err, dirs) {
                dirs = dirs.map(item => path.join(dir, item))
                console.log('dirs=>', dirs)
                let index = 0;

                function next() {
                    console.log('dir=>', dir, cb)
                    if (index === dirs.length) return fs.rmdir(dir, cb)
                    const currentPath = dirs[index++]
                    console.log('currentPaht', currentPath)
                    preDeep(currentPath, next)
                }
                next()
            })
        }
    })
}

preDeep(path.resolve(__dirname, 'a'), function () {
    console.log('删除成功')
})

// fs.rmdir(path.resolve(__dirname,'a/b'), ()=>{
//     console.log('删除成功')
// })
